<?php
class Mqlquangcao extends CI_Model {

    function __construct() {
        parent::__construct();
         $this->load->database();
    }
       function __destruct() {
        $this->db->close();
    }
     function get_collate($str)
    {
        $encoding = strtolower(mb_detect_encoding($str));
        $collate = ($encoding == 'utf-8')?' COLLATE utf8_bin ':'';
        return $collate;
    } 
    function listAll($filter) {
        $sql = "SELECT * FROM advertise as a";

        $where = array();
        if (isset($filter['name']) && $filter['name'] != '0') {
            $collate = $this->get_collate($filter['name']);
            $where[] = "LOWER(a.ads_name) $collate LIKE LOWER('%" . $filter['name'] . "%')";
        }
        if ($where) {
            $sql .= " WHERE " . implode(" AND ", $where);
        }
        $sort_array = array(
            'ads_name',
            'upload_time',
        );
        if (isset($filter['sort']) && in_array($filter['sort'], $sort_array)) {
            $sql .= " ORDER BY a." . $filter['sort'];
        } else {
            $sql .= " ORDER BY a.upload_time";
        }
        if (isset($filter['order']) && $filter['order'] == 'ASC') {
            $sql .= " ASC";
        } else {
            $sql .= " DESC";
        }
        if (isset($filter['limit'])) {
            if ($filter['start'] < 0) {
                $filter['start'] = 0;
            }
            if ($filter['limit'] < 0) {
                $filter['limit'] = 10;
            }
            $sql .= " LIMIT " . (int) $filter['start'] . "," . (int) $filter['limit'];
        }
        $query = $this->db->query($sql);
        if ($query->num_rows() > 0) {
            return $query->result();
        } else {
            return false;
        }
    }
    
    function totalquangcao($filter) {
        $sql = "SELECT count(*) as total FROM advertise as a";

         $where = array();
        if (isset($filter['name']) && $filter['name'] != '0') {
            $collate = $this->get_collate($filter['name']);
            $where[] = "LOWER(a.ads_name) $collate LIKE LOWER('%" . $filter['name'] . "%')";
        }
        if ($where) {
            $sql .= " WHERE " . implode(" AND ", $where);
        }
        $query = $this->db->query($sql);
        if ($query->num_rows() > 0) {
            return $query->row()->total;
        } else {
            return false;
        }
    }
        function delete($id) {
        $this->db->where('id', $id);
        $this->db->delete('advertise');
    }
    
    function update($id, $data) {
        $this->db->where('id', $id);
        $this->db->update('advertise', $data);
    }
        function getInfo($id) {
        $this->db->where('id', $id);
        $query = $this->db->get('advertise');
        if ($query->num_rows() > 0) {
            return $query->row();
        } else {
            $result = new stdClass();
        }
    }
     function add($data) {
        $this->db->insert('advertise', $data);
    }


}